package com.qqbot.impl.task;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.qqbot.impl.service.DuJiaoShouApiService;
import com.qqbot.core.core.Bot;
import com.qqbot.core.core.BotFactory;
import com.qqbot.core.message.MessageChain;

import java.util.List;

/**
 * 每日早晚安推送
 *
 * @author changwenpeng
 * @date 2022年5月9日
 */
@Component
public class RobotTaskMN {

    @Value("${mybot.morningAndNightGroups}")
    Long[] morningAndNightGroups;

    @Autowired
    DuJiaoShouApiService duJiaoShouApiService;

    /**
     * 每日早安
     *
     * @throws Exception
     */
    @Scheduled(cron = "0 0 07 * * ?")
    public void morning() throws Exception {
        MessageChain mc = duJiaoShouApiService.morning();
        this.sendMorningAndNight(mc);

    }

    /**
     * 每日晚安
     *
     * @throws Exception
     */
    @Scheduled(cron = "00 20 22 * * ?")
    public void night() throws Exception {
        MessageChain mc = duJiaoShouApiService.night();
        this.sendMorningAndNight(mc);
    }

    /**
     * 发送消息
     *
     * @param mc
     * @throws Exception
     */
    private void sendMorningAndNight(MessageChain mc) throws Exception {
        List<Bot> botList = BotFactory.getBots();
        if (CollectionUtils.isEmpty(botList)) {
            return;
        }
        Bot bot = botList.get(0);
        for (Long groupId : morningAndNightGroups) {
            System.out.println(mc.toMessageString());
            bot.sendGroupMessage(groupId, mc);
        }
    }

}
